//
//  BusnessTwoVC.m
//  JinChengYun
//
//  Created by 金橙云 on 2017/9/12.
//  Copyright © 2017年 金橙云. All rights reserved.
//

#import "BusnessTwoVC.h"

#import "BusnessTwoCell.h"
#import "TodoHttpManager.h"
#import "ZhuShouThreeVC.h"
#import "LXAlertView.h"
@interface BusnessTwoVC ()<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic,strong)UITableView *tabView;
@property(nonatomic,strong)NSMutableArray *busXArr;


@end

@implementation BusnessTwoVC
-(void)viewDidAppear:(BOOL)animated
{
    //商业房源
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didTDCeoCeBusness:) name:TDCeoCeBusness object:nil];
    if (_busXArr.count == 0)
    {
        // TODO:商业房源
        [_manager setUpCeoShenPiBussnessWithID:_busLouStr];
    }
  
    
    //优惠政策执行
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didAgreeAndUnAgree:) name:TDCeoCeAgree object:nil];
    //优惠政策驳回
     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didAgreeAndUnAgree:) name:TDCeoCeUNAgree object:nil];
    
    
}
// 商业房源
-(void)didTDCeoCeBusness:(NSNotification *)notifi
{
    if (notifi.object != 0)
    {
        _busXArr = [notifi.object objectForKey:@"data"];
        NSLog(@"------商业信息   %@----",_busXArr);
        [_tabView reloadData];
    }
}


//优惠政策的执行OR驳回
-(void)didAgreeAndUnAgree:(NSNotification *)not
{
    if (not.object !=0 )
    {
        NSString *status = [not.object objectForKey:@"status"];
        NSString *info = [not.object objectForKey:@"info"];
        if ([status isEqualToString:@"1"])
        {
            [self.navigationController popViewControllerAnimated:NO];
        }
        else
        {
            [_manager MakeMyselfAlert:info andIsNeedTitle:NO andShowTime:2];
        }
    }
}


-(void)viewDidDisappear:(BOOL)animated
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:TDCeoCeBusness object:nil];
     [[NSNotificationCenter defaultCenter] removeObserver:self name:TDCeoCeAgree object:nil];
     [[NSNotificationCenter defaultCenter] removeObserver:self name:TDCeoCeUNAgree object:nil];
}




- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"优惠政策审批";
    self.view.backgroundColor = BgColor;
    _manager = [TodoHttpManager sharedManager];
    _busXArr = [NSMutableArray array];
    [self setUPBusnessUI];
    
    _tabView = [[UITableView alloc] initWithFrame:CGRectMake(0, 106, B_WIDTH, B_HEIGHT-108) style:UITableViewStylePlain];
    _tabView.delegate = self;
    _tabView.dataSource = self;
    _tabView.rowHeight = 40;
    _tabView.backgroundColor = BgColor;
    _tabView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    [self.view addSubview:_tabView];
    

}

-(void)setUPBusnessUI
{
    UIView *aVi = [[UIView alloc] initWithFrame:CGRectMake(0, 64, B_WIDTH, 40)];
    aVi.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:aVi];
    NSArray *titArr = @[@"楼号",@"房号",@"预售面积",@"优惠政策"];
    UILabel * tempView = [[UILabel alloc]init];
    // NSInteger count = 5;//设置一排view的个数
    NSInteger margin = 10;//设置相隔距离
    NSInteger height = 20;//设置view的高度
    for (int i = 0; i < titArr.count; i ++) {
        //UIView * view = [[UIView alloc]init];
        //view.backgroundColor = [UIColor brownColor];
        UILabel *lab = [[UILabel alloc] init];
        lab.textColor = [UIColor orangeColor];
        lab.font = [UIFont systemFontOfSize:14];
        lab.textAlignment = NSTextAlignmentCenter;
        lab.text = titArr[i];
        [aVi addSubview:lab];
        
        
        //[aVi addSubview:view];
        if (i == 0) {
            [lab mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(aVi).offset(margin);
                make.centerY.equalTo(aVi);
                make.height.mas_equalTo(height);
            }];
        }
        else if (i == titArr.count -1){
            [lab mas_makeConstraints:^(MASConstraintMaker *make) {
                make.right.equalTo(aVi).offset(-margin);
                make.left.equalTo(tempView.mas_right).offset(margin);
                make.centerY.equalTo(tempView);
                make.height.equalTo(tempView);
                make.width.equalTo(tempView);
            }];
        }
        else{
            [lab mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(tempView.mas_right).offset(margin);
                make.centerY.equalTo(tempView);
                make.height.equalTo(tempView);
                make.width.equalTo(tempView);
            }];
        }
        tempView = lab;
        [lab layoutIfNeeded];
    }
    
    

}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _busXArr.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    static  NSString *ID = @"Cell";
    BusnessTwoCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (!cell)
    {
        cell = [[BusnessTwoCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
    }
    if ([cell isKindOfClass:[BusnessTwoCell class]])
    {
                if (_busXArr.count !=0)
                {
                    NSDictionary *dict = [_busXArr objectAtIndex:indexPath.row];
                    [(BusnessTwoCell *)cell setUpCeoBusnessTwoCellFloorStr:dict[@"block_num"] withHouseLab:dict[@"room_num"] withYueLab:dict[@"n"] withWuCeStr:dict[@"name"]];
                }
        //
    }
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    return cell;
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
     ZhuShouThreeVC *ThreeVC = [[ZhuShouThreeVC alloc] init];
    ThreeVC.allID = [[_busXArr objectAtIndex:indexPath.row] objectForKey:@"under_id"];
    //    zhuXVC.typeStr =  [[_zoArr objectAtIndex:indexPath.row] objectForKey:@"shenpi_status"];
    [self.navigationController pushViewController:ThreeVC animated:NO];
    
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    UIView *busnessFootView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, B_WIDTH, 40)];
    busnessFootView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:busnessFootView];
    
    UILabel *linLab = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, B_WIDTH, 1)];
    linLab.backgroundColor = [UIColor lightGrayColor];
    [busnessFootView addSubview:linLab];
    UIButton *busnessAgreeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    //agreeBtn.frame = CGRectMake(20, 5, 80, 30);
    busnessAgreeBtn.layer.cornerRadius = 5;
    busnessAgreeBtn.titleLabel.font = [UIFont systemFontOfSize:14];
    [busnessAgreeBtn setTitle:@"同意执行" forState:0];
    [busnessAgreeBtn setTitleColor:[UIColor whiteColor] forState:0];
    [busnessAgreeBtn setBackgroundColor:[UIColor orangeColor]];
    [busnessAgreeBtn addTarget:self action:@selector(busnessAgreeBtn:) forControlEvents:UIControlEventTouchUpInside];
    [busnessFootView addSubview:busnessAgreeBtn];
    [busnessAgreeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(80, 30));
        make.right.equalTo(busnessFootView.mas_centerX).offset(-10);
        make.top.equalTo(busnessFootView.mas_top).offset(5);
    }];
    
    UIButton *unAgreeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    unAgreeBtn.layer.cornerRadius = 5;
    unAgreeBtn.titleLabel.font = [UIFont systemFontOfSize:14];
    [unAgreeBtn setTitle:@"驳回修改" forState:0];
    [unAgreeBtn setTitleColor:[UIColor whiteColor] forState:0];
    [unAgreeBtn setBackgroundColor:[UIColor orangeColor]];
    [unAgreeBtn addTarget:self action:@selector(busnessUnAgreeBtn:) forControlEvents:UIControlEventTouchUpInside];
    [busnessFootView addSubview:unAgreeBtn];
    [unAgreeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(80, 30));
        make.left.equalTo(busnessFootView.mas_centerX).offset(10);
        make.top.equalTo(busnessFootView.mas_top).offset(5);
    }];
    return busnessFootView;
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    if ([_piTypeStr isEqualToString:@"1"])
    {
        return 40;
    }
    else
    {
        return 0;
    }
}
#pragma mark-----底部的按钮点击事件

-(void)busnessAgreeBtn:(UIButton *)btn
{
    
    LXAlertView *agreeView = [[LXAlertView alloc] initWithTitle:@"提示" message:@"同意执行?" cancelBtnTitle:@"取消" otherBtnTitle:@"确认" clickIndexBlock:^(NSInteger clickIndex) {
        
        if (clickIndex == 1)
        {     NSString *IDA = [[NSUserDefaults standardUserDefaults] objectForKey:@"STATE"];
            
            [_manager setUpCeoShenPiYouHuiAgreeWithType:@"2" withProjectID:_busLouStr withUID:IDA];
            [self.navigationController popViewControllerAnimated:NO];
        }
        
        
        
    }];
    [agreeView showLXAlertView];
    
   
    
}
-(void)busnessUnAgreeBtn:(UIButton *)btn
{
    
    LXAlertView *agreeView = [[LXAlertView alloc] initWithTitle:@"提示" message:@"驳回修改?" cancelBtnTitle:@"取消" otherBtnTitle:@"确认" clickIndexBlock:^(NSInteger clickIndex) {
        
        if (clickIndex == 1)
        {
            NSString *IDA = [[NSUserDefaults standardUserDefaults] objectForKey:@"STATE"];
            [_manager setUPCeoYouHUIPiUnAgreeWithType:@"2" WithID:_busLouStr withUid:IDA];
            [self.navigationController popViewControllerAnimated:NO];
        }
        
        
        
    }];
    [agreeView showLXAlertView];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
